package sms_interface;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * 
 * @author aayushjain
 * Used to receive sms from the GSM gateway, processing the SMS, forming the Http query,
 * using the coordinator to get the sms with the result and sending it to the user. 
 */
public class Get_sms {
	/**
	 * Runs every 10 seconds to check whether there is a sms received or not.
	 * @throws Exception
	 */
	public static void receive_sms() throws Exception
	{
	while(true)
	{
	/*Runtime r = Runtime.getRuntime();
	Process p  = r.exec("wget --no-check-certificate https://admin:99admin11@192.168.1.175/admin/forms/sms-read.html");
	p.waitFor();*/
	File F = new File("sms-read.html");
	DataInputStream din = new DataInputStream(new FileInputStream(F));
	String data = "";
	String arr[] = null;
	
		while((data = din.readLine())!=null)
		{
			//data = din.readLine();
			//Date d ;
			if(data.contains("<script>linie=new Array("))
			{
				arr = data.split(",");
				String dateinstr = arr[0].substring(25, 42);
				//System.out.println(dateinstr);
				String message = arr[2].substring(0, arr[2].length()-21);
				
				if(message.substring(1,6).equalsIgnoreCase("EN%3A"))
				{
					String messageno[] = arr[3].split("/");
					String count = messageno[3].substring(0,7);
					String number = arr[1].substring(3,arr[1].length()-1);
					
					
					/*if(count.charAt(0) == '-')
					{
						count = messageno[3].substring(0,8);
					}*/
					System.out.println("count = " + count);
					System.out.println("message = " + message);
					System.out.println("number = " + number);
					MessageVerification mv=new MessageVerification();
					String app_id=null;
					String message2="sms received";
					int flag=0;
					
					boolean pass2=mv.verify(message);
					String cr=mv.getclass();
					app_id=mv.getAppliance();
					System.out.println(cr);
					String Au_Name=null;
					if(pass2==false)
					{
						message2="Format error. Resend message with correct format.";
					}
					else{
						UserAuthentication ua=new UserAuthentication();
						boolean pass=ua.authenticate(message,cr);
						Au_Name=ua.getName();
						if(pass==true){
							flag=1;
							//message2="Sms received";
							
						}
						else{
							flag=0;
							message2="You are not authenticated to do this";	
							
						}						
					}
					if(flag==1){
						String RestURL=RestQueryFormation.formQuery(message,dateinstr);
						System.out.println(RestURL);
						Coordinator c=new Coordinator();
						message2=c.coordinate(RestURL);
						String query2="Insert into log_sms values('"+Au_Name+"','"+Sending_sms_mysql.date()+"','"+Sending_sms_mysql.now()+"','"+message2+"','"+app_id+"')";
			            LogEntryGood leg=new LogEntryGood();
			            leg.enter(query2);
					}
					
					
					Sending_sms_mysql abc=new Sending_sms_mysql();
					number="0"+number;
					System.out.println(message2);
					abc.send_sms(number,message2);
					/*p = r.exec("wget --no-check-certificate https://admin:99admin11@192.168.1.175/admin/forms/sms-read.html?id=/out/sms/"+count+"&send=del");
						p.waitFor();
						File D = new File("sms-read.html?id=%2Fout%2Fsms%2F"+count+"&send=del");
						D.delete();*/			
				}
			}
		}
		din.close();
		//F.delete();
		System.out.println("Waiting");
		Thread.sleep(5000);
		}
	}	
	
	/**
	 * calls the function receive_sms. For instructions refer to receive_sms.
	 * @param args
	 * @throws Exception
	 */
public static void main(String args[]) throws Exception
{
	receive_sms();
}
}
